<template>
  <div style="background-color: #FFF;padding: 50px">
    <div class="ana_div">
      <div class="title">
        <span>用户增长</span>
      </div>
      <!--昨日数据-->
      <div >
        <!--昨日用户数据-->
        <div class="show_div show_yes">
          <div class="div-title">
            <span>昨日用户指标</span>
          </div>
          <div class="show-div">
            <div class="detail_div show-boder">
              <div>
                <span>
                  新加入人数
                </span>
              </div>
              <div>
                <span>
                  0
                </span>
              </div>
              <div></div>
            </div>
            <div class="detail_div show-boder">
              <div>
                <span>
                  累计学生数
                </span>
              </div>
              <div>
                <span>
                  0
                </span>
              </div>
              <div></div>
            </div>
            <div class="detail_div show-boder">
              <div>
                <span>
                  累计教师数
                </span>
              </div>
              <div>
                <span>
                  0
                </span>
              </div>
              <div></div>
            </div>
            <div class="detail_div">
              <div>
                <span>
                  累计用户数
                </span>
              </div>
              <div>
                <span>
                  0
                </span>
              </div>
              <div></div>
            </div>
          </div>
        </div>
        <!--昨日用户数据-END-->
        <!--新增用户趋势图-->
        <div class="show_div show_yes">
          <div class="div-title">
            <span>
              新增用户趋势图
            </span>
          </div>
          <div class="show-div">
            <div id="show_trend" style="width: 100%;height: 300px">
            </div>
          </div>
        </div>
        <!--新增用户趋势图-END-->
      </div>
    </div>
    <div class="ana_div">
      <div class="title">
        <span>用户属性</span>
      </div>
      <div class="userInfo">
        <div class="info_div">
          <div class="detail_div image_div" id="image_div"></div>
          <div class="detail_div office_div" id="office_div"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'UserAnalysis',
  data () {
    return {
      day: [7, 15, 30],
      qday: 30,
      trendData: {
        xdata: [],
        ydata: []
      },
      sexData: [],
      officeData: []
    }
  },
  mounted () {
    this.getNewUser()
    this.getAnaSex()
    this.getAnaOffice()
  },
  methods: {
    getNewUser () {
      var _this = this
      this.$axios.post('/api/user/getNewUser',
        this.$qs.stringify({
          day: this.qday
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            // _this.trendData = data.result
            for (var i = 0; i < data.result.length; i++) {
              _this.trendData.ydata.push(data.result[i].count)
              _this.trendData.xdata.push(data.result[i].date)
            }
            _this.trendData.ydata = _this.trendData.ydata.reverse()
            _this.trendData.xdata = _this.trendData.xdata.reverse()
            _this.drawTrend()
          }
        })
    },

    getAnaSex () {
      var _this = this
      this.$axios.post('/api/user/getAnaSex'
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.sexData = data.result
            _this.drawSex()
          }
        })
    },
    getAnaOffice () {
      var _this = this
      this.$axios.post('/api/user/getAnaOffice'
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.officeData = data.result
            _this.drawOffice()
          }
        })
    },
    drawTrend () {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('show_trend'))
      // 绘制图表
      myChart.setOption({
        title: { text: '趋势图' },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            show: true,
            type: 'line',
            snap: true,
            axis: 'x'
          }
        },
        xAxis: {
          data: this.trendData.xdata
        },
        yAxis: {},
        series: [{
          name: '新增人数',
          type: 'line',
          // smooth: true,
          // symbol: 'none',
          stack: 'a',
          areaStyle: {
            normal: {}
          },
          data: this.trendData.ydata
          // itemStyle: {normal: {label: {show: true}}}
        }]
      })
    },
    drawSex () {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('image_div'), 'light')
      myChart.setOption({
        title: {
          text: '用户性别'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          top: 10,
          right: 10,
          data: ['男', '女', '保密']
        },
        series: [
          {
            name: '用户性别',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: 'center',
              normal: {
                formatter: '{b}  {d}%  '
              }
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '30',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: true
            },
            data: this.sexData
            // data: [
            //   {value: 335, name: '男'},
            //   {value: 310, name: '女'},
            //   {value: 234, name: '未知'}
            // ]
          }
        ]
      })
    },
    drawOffice () {
      // 基于准备好的dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('office_div'), 'light')
      myChart.setOption({
        title: {
          text: '用户职业'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          top: 10,
          right: 10,
          data: ['学生', 'Python工程师', 'Web前端工程师', 'JS工程师', 'PHP开发工程师', 'JAVA开发工程师', '移动开发工程师', 'Linux系统工程师', '产品经理', '数据库工程师', '软件工程师', 'UI设计师', '交互设计师', 'CG影视动画师', '全栈工程师', '算法工程师', '架构师']
        },
        series: [
          {
            name: '用户职业',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: 'center',
              normal: {
                formatter: '{b}  {d}%  '
              }
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '30',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: true
            },
            data: this.officeData
            // data: [
            //   {value: 321, name: '学生'},
            //   {value: 321, name: 'Python工程师'},
            //   {value: 321, name: 'Web前端工程师'},
            //   {value: 321, name: 'JS工程师'},
            //   {value: 321, name: 'PHP开发工程师'},
            //   {value: 321, name: 'JAVA开发工程师'},
            //   {value: 321, name: '移动开发工程师'},
            //   {value: 321, name: 'Linux系统工程师'},
            //   {value: 321, name: '产品经理'}
            // ]
          }
        ]
      })
    }
  }
}
</script>

<style>
  .ana_div{
  }
  .show_div {
    border: 1px solid #e7e7eb;
    margin-bottom: 20px;
  }
  .ana_div .title{
    font-size: 26px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  .show_yes .div-title{
    background-color: #f4f5f9;
    border-bottom: 1px solid #e7e7eb;
    height: 35px;
    line-height: 35px;
    padding-left: 20px;
  }
  .show_yes .show-div{
    margin: 20px;
  }
  .show_yes .detail_div{
    display: inline-block;
    width: 24%;
    text-align: center;
  }
  .show_yes .show-boder{
    border-right: 1px solid #e7e7eb;
  }
  .userInfo .info_div {
    padding: 20px;
  }
  .info_div .detail_div{
    display: inline-block;
    height: 250px;
    margin-bottom: 50px;
  }
  #image_div{
    width: 100%;
  }
  #office_div{
    width: 100%;
  }
</style>
